package com.example.shopping.app;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.example.shopping.R;
import com.example.shopping.bean.Response;
import com.example.shopping.utils.Constants;
import com.example.shopping.utils.CountDownTimerUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import okhttp3.Call;


public class  ForgetActivity extends AppCompatActivity {

    private TextView textView;

    private TextView forget_email;

    private TextView forget_code;

    private Button btn_verification;

    private String forget_response = null;
    // 验证结果
    private boolean requestResult = true;

    private String email;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        textView =  findViewById(R.id.code);
        forget_email = findViewById(R.id.email);
        forget_code = findViewById(R.id.forget_code);
        btn_verification = findViewById(R.id.btn_verification);

        // 设置点击事件
        initView();


    }

    protected void initView() {

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 点击事件
                String url = Constants.Login_GetVerificationCode;
                email = forget_email.getText().toString();
                HashMap<String, String> map = new HashMap<>();
                map.put("email",email);
                OkHttpUtils
                        .get()
                        .url(url)
                        .params(map)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                Log.e("onError",e.getMessage());

                            }

                            @Override
                            public void onResponse(String response, int id) {
                                Log.e("onResponse",response);
                            }
                        });

                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(textView, 60000, 1000);
                mCountDownTimerUtils.start();
            }
        });


        // 判断是否验证成功
        btn_verification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String code = forget_code.getText().toString();
                email = forget_email.getText().toString();
                if(code != null){

                    HashMap<String, String> map = new HashMap<>();

                    map.put("email",email);
                    map.put("code",code);
                    OkHttpUtils
                            .get()
                            .url(Constants.Login_ForgetPassword)
                            .params(map)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    Log.e("BtnOnError",e.getMessage());
                                    requestResult = false;
                                }

                                @Override
                                public void onResponse(String response, int id) {

                                    Log.e("BtnOnResponse",response);
                                    forget_response = response;
                                    if(response != null){
                                        toModify();
                                    }
                                }
                            });
                }else {
                    return;
                }



            }
        });




    }


    // 验证成功，前往修改密码界面
    public void toModify(){
        email = forget_email.getText().toString();
        Response response = JSON.parseObject(forget_response, Response.class);

        if(response.getCode() == 200000){
            Toast.makeText(this,response.getMessage(),Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, ModifyActivity.class);
            intent.putExtra("email",email);
            startActivity(intent);
        }
        else {
            Toast.makeText(this,response.getMessage(),Toast.LENGTH_SHORT).show();
            return;
        }

    }
}
